﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace XYS.Core.Util.Helper
{
    /// <summary>
    /// 加密 帮助类
    /// </summary>
    public partial class EncryptHelper
    {
        /// <summary>
        /// RSA 私钥
        /// </summary>
        public static string RSAPrivateKey = "";
        /// <summary>
        /// RSA 公钥
        /// </summary>
        public static string RSAPublicKey = "";

        /// <summary>
        /// SHA512 加密
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string SHA512Encrypt(string value)
        {
            if (string.IsNullOrWhiteSpace(value)) return "";

            var bytes = Encoding.UTF8.GetBytes(value);
            using var sha512 = SHA512.Create();
            var encryptBytes = sha512.ComputeHash(bytes);
            var result = string.Join("", encryptBytes.Select(x => x.ToString("x2")));
            return result;
        }

        /// <summary>
        /// MD5 编码
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string MD5Encrypt(string value)
        {
            if (string.IsNullOrWhiteSpace(value)) return "";

            var encryptBytes = MD5.HashData(Encoding.Default.GetBytes(value));
            var result = string.Join("", encryptBytes.Select(x => x.ToString("x2")));
            return result;
        }

        /// <summary>
        /// MD5 编码
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static string MD5Stream(Stream stream)
        {
            if (stream != null)
            {
                using MD5 md5 = MD5.Create();
                var bytes = md5.ComputeHash(stream);
                StringBuilder sb = new();
                for (int i = 0; i < bytes.Length; i++)
                {
                    sb.Append(bytes[i].ToString("x2"));
                }
                return sb.ToString().ToUpper();
            }
            return "";
        }

        /// <summary>
        /// MD5 编码
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string MD5Stream(string filePath)
        {
            if (!string.IsNullOrWhiteSpace(filePath) && File.Exists(filePath))
            {
                using FileStream stream = File.Open(filePath, FileMode.Open);
                return MD5Stream(stream);
            }
            return "";
        }



        //默认DES密钥向量
        private static readonly byte[] RgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        private const string DESKey = "CQYRZHXG";
        /// <summary>
        /// DES 加密
        /// </summary>
        /// <param name="value"></param>
        /// <param name="encryptKey"></param>
        /// <returns></returns>
        public static string DESEncrypt(string value, string encryptKey = DESKey)
        {
            if (!string.IsNullOrWhiteSpace(value))
            {
                using DES des = DES.Create();
                var bytes = Encoding.Default.GetBytes(value);

                byte[] rgbKey = Encoding.Default.GetBytes(encryptKey[..8]);

                MemoryStream mStream = new();
                CryptoStream cStream = new(mStream, des.CreateEncryptor(rgbKey, RgbIV), CryptoStreamMode.Write);
                cStream.Write(bytes, 0, bytes.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            else
            {
                return "";
            }
        }




        /// <summary>
        /// RSA加密
        /// </summary>
        /// <param name="content">加密内容</param>
        /// <returns></returns>
        public static string RSAEncrypt(string content)
        {
            if (string.IsNullOrWhiteSpace(RSAPublicKey)) return null;
            using RSACryptoServiceProvider rsa = new();
            byte[] cipherbytes;
            rsa.FromXmlString(RSAPublicKey);
            cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
            return Convert.ToBase64String(cipherbytes);
        }

        /// <summary>
        /// RSA解密
        /// </summary>
        /// <param name="content">解密内容</param>
        /// <returns></returns>
        public static string RSADecrypt(string content)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(RSAPrivateKey)) return null;
                using RSACryptoServiceProvider rsa = new();
                byte[] cipherbytes;
                rsa.FromXmlString(RSAPrivateKey);
                cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);

                return Encoding.UTF8.GetString(cipherbytes);
            }
            catch { }
            return null;
        }


    }
}
